
<template>
  <div class="home">
    <el-form :model="form" :inline="true" >
    <p>这个只支持手机号和密码登录,一个比较简单的网易云删评论工具,自用</p>
       <el-form-item label="手机号">
        <el-input v-model="form.username" placeholder="手机号" ></el-input>
       </el-form-item>
       <el-form-item label="密码">
        <el-input v-model="form.password" placeholder="密码" ></el-input>
       </el-form-item>
       <el-form-item>
        <el-button type="primary" @click="loginFN" :loading="loading">登录(不要重复登录，会风控)</el-button>
        <el-button type="primary" @click="getCommentList" :loading="loading">获取评论</el-button>
       </el-form-item>
       <el-form-item>
        uid:{{uid||"未登录"}} 
       </el-form-item>
    </el-form>
    <el-table :data="dataList"  style="width: 100%" resizable :border="true">
      <el-table-column prop="threadId" label="threadId" width="200px" />
      <el-table-column prop="resourceInfo.id" label="资源ID" width="100px" />
      <el-table-column prop="resourceType" label="资源类型"  width="100px" />
      <el-table-column prop="commentId" label="评论ID"  width="100px" />
      <el-table-column prop="content" label="评论内容" />
      <el-table-column prop="time" label="评论时间" width="420px"  >
        <template #default="scope">
        {{new Date(scope.row.time)}}
        </template>  
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
      <template #default="scope">
        <span >
          <el-button type="text" size="small" @click.prevent="deleteRow(scope.row)">删0</el-button>
        </span>
      </template>
    </el-table-column>
    </el-table>
  </div>
</template>
<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { ref,reactive,onBeforeMount } from 'vue'
import api from '@/api'

 

  const form = reactive({
    username: '',
    password: '',
  })


  const dataList = ref([])
  const loading = ref(false)
  const uid = ref('')

   onBeforeMount(()=>{
    uid.value = window.localStorage.getItem('uid')
  })


  const loginFN = () =>{
    if(!form.username || !form.password){
      alert("请输入账号密码")
      return
    }
    api.DeleteCommentNetease_login({
      phone:form.username,
      password:form.password
    }).then((res:any)=>{
        console.info(res)
        uid.value = res.account.id
        window.localStorage.setItem('uid',res.account.id)
    })
  } 

  const getCommentList = () =>{

    if(!uid.value){
      alert("没有登录!")
      return;
    }
    let form:any = {
      uid:uid.value,
      limit:10,
      timestamp:new Date().getTime()
    }

    if(dataList.value.length >0){
      form.time = dataList.value[dataList.value.length - 1].time
    }

    api.DeleteCommentNetease_getCommentList(form).then((res:any)=>{
        console.info(res)
        const commentsList = res.data.comments
        commentsList.forEach((el:any) => {
          el.resourceInfo = JSON.parse(el.resourceInfo)

        // if(el.resourceType == 5)el.resourceType = 1
        if(el.threadId.indexOf('R_SO_4') !=-1)el.resourceType = 0
        });
        dataList.value = [...dataList.value,...res.data.comments]
        console.info(dataList.value)
    })
  } 

  const deleteRow = (target:any) =>{

    console.info(target)

    let form:any  = {
      t:0,
      type:target.resourceType,
      commentId:target.commentId,
      timestamp:new Date().getTime()
    }

    if(target.resourceInfo){
      form.id = target.resourceInfo.id
    }

    api.DeleteCommentNetease_deleteComment(form).then((res:any)=>{
      if(res.code == 200){
        ElMessage.success("删除成功")
        dataList.value = dataList.value.filter(el=>el != target)
      }else{
        ElMessage.error("删除失败")
      }
      
        console.info(res)
    })
  } 
  

  


</script>
<style lang="scss" scoped>
p{
  font-size: 14px;
}
</style>
